﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace EdgeSearch.Application.Services
{
    public class EdgeSearchServices : IDynamicApiController, IScoped
    {
        public EdgeSearchServices()
        {
        }

        /// <summary>
        ///  PC搜索
        /// </summary>
        /// <param name="Count">搜索次数</param>
        /// <param name="Span">搜索间隔（秒）</param>
        /// <param name="XClientData"></param>
        /// <param name="Cookie"></param>
        /// <returns></returns>
        public async Task<string> SearchPC(int Count=30,int Span=5,string XClientData= "",string Cookie="")
        {
            for (int i = 0; i < Count; i++)
            {
                Console.WriteLine($"开始执行第:{i+1}次搜索");

                var q=Guid.NewGuid().ToString();
                await SendSearchPC(XClientData, Cookie,q);
                await Task.Delay(Span * 1000);
            }
            return "success";
        }

        private async Task SendSearchPC(string XClientData, string Cookie,string q)
        {
            using (var httpClient = new HttpClient())
            {
                var uriBuilder = new UriBuilder($"https://www.bing.com/search?q={q}&form=ANNTH1&refig=671f452f240a440e9a49cc4495f5447e&pc=CNNDDB");
                var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uriBuilder.ToString());
                httpRequestMessage.Headers.Add("Host", "www.bing.com");
                httpRequestMessage.Headers.Add("Connection", "keep-alive");
                httpRequestMessage.Headers.Add("ect", "4g");
                httpRequestMessage.Headers.Add("sec-ch-ua", """Chromium";v="130", "Microsoft Edge";v="130", "Not ? A_Brand";v="99""");
                httpRequestMessage.Headers.Add("sec-ch-ua-mobile", "?0");
                httpRequestMessage.Headers.Add("sec-ch-ua-full-version", """130.0.2849.56""");
                httpRequestMessage.Headers.Add("sec-ch-ua-arch", """x86""");
                httpRequestMessage.Headers.Add("sec-ch-ua-platform", """Windows""");
                httpRequestMessage.Headers.Add("sec-ch-ua-platform-version", """15.0.0""");
                httpRequestMessage.Headers.Add("sec-ch-ua-model", "");

                httpRequestMessage.Headers.Add("sec-ch-ua-bitness", """64""");
                httpRequestMessage.Headers.Add("sec-ch-ua-full-version-list", """Chromium";v="130.0.6723.70", "Microsoft Edge";v="130.0.2849.56", "Not ? A_Brand";v="99.0.0.0""");
                httpRequestMessage.Headers.Add("DNT", "1");
                httpRequestMessage.Headers.Add("Upgrade-Insecure-Requests", "1");
                httpRequestMessage.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0");
                httpRequestMessage.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
                httpRequestMessage.Headers.Add("X-Edge-Shopping-Flag", "0");
                httpRequestMessage.Headers.Add("Sec-MS-GEC", "3B8170DD690DCFC4AC3610361AEFC1063B756B79D46750C05314037E515B0BCD");
                httpRequestMessage.Headers.Add("Sec-MS-GEC-Version", "1-130.0.2849.56");
                httpRequestMessage.Headers.Add("X-Client-Data", XClientData);
                httpRequestMessage.Headers.Add("Sec-Fetch-Site", "cross-site");
                httpRequestMessage.Headers.Add("Sec-Fetch-Mode", "navigate");
                httpRequestMessage.Headers.Add("Sec-Fetch-User", "?1");
                httpRequestMessage.Headers.Add("Sec-Fetch-Dest", "document");
                httpRequestMessage.Headers.Add("Referer", "https://ntp.msn.cn/");
                httpRequestMessage.Headers.Add("Accept-Encoding", "gzip, deflate, br, zstd");
                httpRequestMessage.Headers.Add("Accept-Language", "zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7");
                httpRequestMessage.Headers.Add("Cookie", Cookie);
                var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);
                var httpContent = httpResponseMessage.Content;
                string result = await httpResponseMessage.Content.ReadAsStringAsync();
                //Console.WriteLine(result);
            }
        }


        public string SearchAPP()
        {
            return null;
        }

    }
}
